package net.towee.client.widgets;

import net.towee.client.resources.ClientResource;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.inject.Inject;

public class LoadingBar extends PopupPanel {
	private Timer showTimer = null;
	
	@Inject
	public LoadingBar(ClientResource resource) {
		super();
		
		setAutoHideEnabled(false);
		setGlassEnabled(true);
		
		Image imgContainer = new Image( resource.loading() );
		setWidget( imgContainer );
		setStyleName( resource.css().loadingBar() );
		setGlassStyleName( resource.css().loadingBarGlass() );
	}
	
	public void toggle() {
		if(showTimer != null)
			showTimer.cancel();
		
		if(!isShowing() && showTimer == null) {
			showTimer = new Timer(){
				@Override
				public void run() {
					center();
					showTimer = null;
				}
			};
			
			showTimer.schedule(600);
		}
		else {
			hide();
			showTimer = null;
		}
	}
}
